<?php
function addCart(){
    //准备要添加的购物车数据
    $arr['pid']=intval($_POST['productid']);
    $arr['num']=intval($_POST['num']);
    $arr['price']=$_POST['price'];
    $arr['userId']=intval($_SESSION['loginFlag']);
    $arr['createTime']=time();
    //完成购物车数据的添加操作
    $userId=$arr['userId'];
    $pid=$arr['pid'];
    $where="userId=$userId and pid=$pid";
    $sql="select num from stshop_cart where ".$where;
    $rows=fetchOne($sql);
    if($rows){
        $arr['num']+=$rows['num'];
        $res=update("stshop_cart",$arr,$where);
    }else{
        $res=insert("stshop_cart",$arr);
    }
    //返回最终添加的结果
    if($res){
        $response=array(
            'errno'=>0,
            'errmsg'=>'success',
            'data'=>true,
        );
    }else{
        $response=array(
            'errno'=>-1,
            'errmsg'=>'fail',
            'data'=>false,
        );
    }
    return $response;
}
function changeNum(){
    $arr['pid']=intval($_POST['productid']);
    $arr['num']=intval($_POST['num']);
    $arr['userId']=intval($_SESSION['loginFlag']);
    
    $userId=$arr['userId'];
    $pid=$arr['pid'];
    $where="userId=$userId and pid=$pid";
    $sql="select num from stshop_cart where ".$where;
    $rows=fetchOne($sql);
    if($rows['num']!=$arr['num']){
        $res=update("stshop_cart",$arr,$where);
    }
    if($res){
        $response=array(
            'errno'=>0,
            'errmsg'=>'success',
            'data'=>true,
        );
    }else{
        $response=array(
            'errno'=>-1,
            'errmsg'=>'fail',
            'data'=>false,
        );
    }
    return $response;
}
function clearCart(){
    $arr['userId']=intval($_SESSION['loginFlag']);
    
    $userId=$arr['userId'];
    $where="userId=$userId";
    $sql="select * from stshop_cart where ".$where;
    $rows=fetchOne($sql);
    if($rows){
        $res=delete("stshop_cart",$where);
    }
    if($res){
        $response=array(
            'errno'=>0,
            'errmsg'=>'success',
            'data'=>true,
        );
    }else{
        $response=array(
            'errno'=>-1,
            'errmsg'=>'fail',
            'data'=>false,
        );
    }
    return $response;
}
function deleteProduct(){
    $arr['pid']=intval($_GET['productid']);
    $arr['userId']=intval($_SESSION['loginFlag']);
    
    $userId=$arr['userId'];
    $pid=$arr['pid'];
    $where="userId=$userId and pid=$pid";
    $sql="select num from stshop_cart where ".$where;
    $rows=fetchOne($sql);
    if($rows){
        $res=delete("stshop_cart",$where);
    }
    if($res){
        $response=array(
            'errno'=>0,
            'errmsg'=>'success',
            'data'=>true,
        );
    }else{
        $response=array(
            'errno'=>-1,
            'errmsg'=>'fail',
            'data'=>false,
        );
    }
    return $response;
}
function getAllCartPro(){
    //得到当前登录用户购物车数据库中所有商品
    $userId=intval($_SESSION['loginFlag']);
    $sql="select * from stshop_cart where userId=$userId";
    $rows=fetchAll($sql);
    return $rows;
}
function getAllCartProNum(){
    //得到当前登录用户购物车数据库中所有商品总数量
    $userId=intval($_SESSION['loginFlag']);
    $sql="select sum(num) from stshop_cart where userId=$userId";
    $rows=fetchOne($sql);
    return $rows['sum(num)'];
}
function getAllOrders($orderStatus){
    $userId=intval($_SESSION['loginFlag']);
    if($orderStatus<4){
        $sql="select * from stshop_order where userId=$userId and Status=$orderStatus group by code";
    }else{
        $sql="select * from stshop_order where userId=$userId group by code";
    }
    $rows=fetchAll($sql);
    return $rows;
}
function getOrderByNumber($orderNumber,$orderStatus){
    if($orderStatus<4){
        $sql="select * from stshop_order where code='$orderNumber' and Status=$orderStatus";
    }else{
        $sql="select * from stshop_order where code='$orderNumber'";
    }
    $rows=fetchAll($sql);
    return $rows;
}
function getOrderTotalPrice($code){
    //得到指定订单号的总价格
    $sql="select sum(price) from stshop_order where code='$code'";
    $rows=fetchOne($sql);
    return $rows['sum(price)'];
}